﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MensUnoShop.Models
{
    [Serializable]
    public class Tag
    {
        private long _id;                   //ID
        private int _lv;                   //層級
        private string _title;             //標題
        private string _alt;              //說明
        private string _alias;             //別名
        private long _parentid;             //父ID
        private string _uri;               //連結
        private List<Tag> _childlist;  //子分類
        private Config.TagType _tagtype;
        [NonSerialized]
        private DBTag _Tagrepository;

        public long ID { set { _id = value; } get { return _id; } }
        public long PARENTID { set { _parentid = value; } get { return _parentid; } }
        public int LV { set { _lv = value; } get { return _lv; } }
        public string TITLE { set { _title = value; } get { return _title; } }
        public string ALT { set { _alt = value; } get { return _alt; } }
        public string ALIAS { set { _alias = value; } get { return _alias; } }
        public string URI { set { _uri = value; } get { return _uri; } }
        public List<Tag> CHILDLIST { set { _childlist = value; } get { return _childlist; } }
        public Config.TagType TAGTYPE{ set { _tagtype = value; } get { return _tagtype; } }
        public Tag() { _childlist = null; }

        public void GetChildList()
        {
            setRepository();
            _childlist = _Tagrepository.GetClassTagList(_id);
            foreach (var c in _childlist)
            {
                if (string.IsNullOrEmpty(c.ALIAS))
                    c.URI = this.URI + "/" + c.ID;
                else
                    c.URI = this.URI + "/" + c.ALIAS;
            }
        }

        public void GetChildList(int amount)
        {
            setRepository();
            _childlist = _Tagrepository.GetClassTagList(_id, amount);
            foreach (var c in _childlist)
            {
                if (string.IsNullOrEmpty(c.ALIAS))
                    c.URI = this.URI + "/" + c.ID;
                else
                    c.URI = this.URI + "/" + c.ALIAS;
            }
        }

        /*public void SetRepository(ICatalogRepository repository)
        {
            if(_Catalogrepository==null)
                _Catalogrepository = repository;
        }*/

        private void setRepository()
        {
            if (_Tagrepository == null)
                _Tagrepository = new DBTag();
        }

        //取得分類網址
        public void GetUri()
        {
            setRepository();
            string root = HttpContext.Current.Request.Url.Host;
            try
            {
                if (string.IsNullOrEmpty(_uri))
                {
                    if (_lv == 1)
                        _uri = "http://" + root + "/product/" + _alias;
                    else if (_lv == 2)
                    {
                        Tag p = _Tagrepository.GetTag(_parentid);
                        _uri = "http://" + root + "/product/" + p.ALIAS + "/" + _alias;
                    }
                }

                if (_tagtype == Config.TagType.FREE)
                {
                    Tag p = _Tagrepository.GetTag(_parentid);
                    _uri = "http://" + root + "/product/" + p.ALIAS + "/f/" + _id;
                }

                if (_tagtype == Config.TagType.COMBO)
                {
                    Tag p = _Tagrepository.GetTag(_parentid);
                    _uri = "http://" + root + "/product/" + p.ALIAS + "/c/" + _id;
                }
            }
            catch
            {
                _uri = "/";
            }
        }
    }
}